// 公用css文件
require("../../assets/css/communal.less");

// 引入字体文字
require("../../assets/fonts/fitness-fonts/iconfont.css");

const { default: axios } = require("axios");
// 引入工具js
const dom = require("../../util/dom.js");

// 当前页的css文件
require("./login.less");

// 当前页js
window.addEventListener("DOMContentLoaded", function () {
	// 跳转注册账号
	dom.$("#skip").addEventListener("click", function () {
		location.href = "register.html";
	});

	// 点击眼睛显示密码
	let num = 0;
	dom.$("#eyes").addEventListener("click", function () {
		num++;
		if (num % 2 === 1) {
			dom.$("#paw").type = "text";
			dom.$("#eyes").className = "iconfont icon-yanjing";
		} else {
			dom.$("#paw").type = "password";
			dom.$("#eyes").className = "iconfont icon-biyanjing";
		}
	});

	// 账号的正则
	let reg =
		/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/;

	// 密码的正则
	let reg2 = /^[a-zA-Z]\w{5,17}$/;

	dom.$("#btn").addEventListener("click", function () {
		// 账号验证
		if (reg.test(dom.$("#tel").value)) {
			dom.$(".tel").textContent = "";
		} else {
			dom.$(".tel").textContent = "请输入正确的电话号码";
			dom.$(".tel").style.color = "#f00";
			return;
		}

		// 密码验证
		if (reg2.test(dom.$("#paw").value)) {
			dom.$(".paw").textContent = "";
		} else {
			dom.$(".paw").textContent = "请输入以英文开头5-17位的字符";
			dom.$(".paw").style.color = "#f00";
			return;
		}

		// ajax接口
		axios
			.post(" http://fitness.h5.itsource.cn:3701/api/user/login", {
				account: dom.$("#tel").value,
				password: dom.$("#paw").value,
			})
			.then((res) => {
				if (res.data.errno === 0) {
					localStorage.setItem("token", res.data.data.token);
					location.href = "index.html";
					alert("登陆成功");
				} else {
					alert("登录失败");
				}
			});
	});
});
